package com.qy127.dao.impl;

import com.qy127.dao.MenuDao;
import com.qy127.entity.Menu;
import com.qy127.entity.Tree;
import com.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuDaoImpl
 * @Description ToDo
 * @Auther teacher Bai
 * @Date 2020/12/26 11:12
 */
@Repository
public class MenuDaoImpl implements MenuDao {
    @Override
    public List<Menu> getAllMenuByRid(Integer roleid) {
        //1.先根据roleid 查询所有的父菜单
        String sql = "select tm.mid,tm.mname,tm.mcode,tm.murl,tm.micon from t_role_premission trp INNER JOIN t_menu tm\n" +
                "ON trp.mid=tm.mid \n" +
                "where trp.rid = ? and tm.mpid=0";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql, roleid);//此处获取父菜单信息
        //定义一个大的listMenu存放父级和子级双层菜单信息。
        List<Menu> menus = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            //此处sonMenu先置null,下边查询过子菜单数据后再填充
            Menu menu = new Menu((Integer)map.get("mid"),(String)map.get("mname"),(String)map.get("murl"),null);

            //2.根据父菜单的mid查询子菜单信息
            String sqlSon = "SELECT t2.mname,t2.mid,t2.murl,t2.mcode,t2.micon from t_menu t1 INNER JOIN t_menu t2\n" +
                    "ON t1.mid=t2.mpid  and t1.mid=?";
            //此处根据父菜单mid获取子菜单信息
            List<Map<String, Object>> maps1 = JDBCUtil.queryListMap(sqlSon, map.get("mid"));
            List<Menu> sonMenu = new ArrayList<>();
            //对得到的子菜单遍历 将数据转储成List<Menu>
            for (Map<String, Object> map1 : maps1) {
                Menu menu1 = new Menu((Integer) map1.get("mid"), (String) map1.get("mname"), (String) map1.get("murl"),null);
                sonMenu.add(menu1);
            }
            menu.setSonMenu(sonMenu);
            menus.add(menu);
        }
        return menus;
    }

    @Override
    public List<Tree> getAllMenu() {
        //1.查询所有的父菜单
        String sql = "select mid,mname from t_menu t where t.mpid=0";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql);//此处获取父菜单信息
        //定义一个大的listMenu存放父级和子级双层菜单信息。
        List<Tree> trees = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            //此处sonMenu先置null,下边查询过子菜单数据后再填充
            Tree tree = new Tree((Integer)map.get("mid"),(String)map.get("mname"),null);

            //2.根据父菜单的mid查询子菜单信息
            String sqlSon = "SELECT t2.mid,t2.mname from t_menu t1 INNER JOIN t_menu t2\n" +
                    "ON t1.mid=t2.mpid  and t1.mid=?";
            //此处根据父菜单mid获取子菜单信息
            List<Map<String, Object>> maps1 = JDBCUtil.queryListMap(sqlSon, map.get("mid"));
            List<Tree> sonTree = new ArrayList<>();
            //对得到的子菜单遍历 将数据转储成List<Menu>
            for (Map<String, Object> map1 : maps1) {
                Tree tree1 = new Tree((Integer) map1.get("mid"), (String) map1.get("mname"),null);
                sonTree.add(tree1);
            }
            tree.setChildren(sonTree);
            trees.add(tree);
        }
        return trees;
    }

    @Override
    public List<Map<String,Object>> getAllMidByRid(String rid) {
        String sql = "SELECT trp.mid FROM t_role_premission trp \n" +
                "INNER JOIN t_menu tm ON trp.mid = tm.mid \n" +
                "and tm.mpid!=0 WHERE trp.rid=?";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql, rid);
        return maps;
    }

    @Override
    public void delRolePremissionByRid(String rid) {
            String sql = "delete from t_role_premission where rid = ?";
            JDBCUtil.update(sql,rid);
    }

    @Override
    public void insertRolePremission(String rid, String[] split) {
        for (String s : split) {
            String sql ="insert into t_role_premission values(null,?,?)";
            JDBCUtil.update(sql,rid,s);
        }

    }


}
